<?php
defined('_JEXEC') or die('Restricted access');

class AccessManagerHelper {
    var $_db = null;
    var $_user = null;
    var $_groups = array();
    
    var $_selected_file = null;
    var $_selected_category = null;

    function __construct($file = null, $category = null) {
        $this->_db = JFactory::getDBO();
        $this->_user = JFactory::getUser();
        
        if ($this->_user->id > 0) {
            $this->_groups = $this->_getUserGroups(); 
        }
    }
    
    function _getUserGroups() {
        $query = 'select group_id from #__docario_group_member';
        $query .= ' where userid='.$this->_user->id;
        
        $this->_db->setQuery($query);
        return $this->_db->loadResultArray();
    }
    
    function setFile($file = null) { $this->_selected_file = $file }
    function setCategory($category = null) { 
        $this->_selected_category = $category 
    }
    
    function canRead() {
        if ($this->_selected_file != null) {
            $query = 'select `read` from #__docario_file_user_allowed';
            $query .= ' where userid='.$this->_user->id;
            $query .= ' and file_id='.$this->_selected_file->id;
            
            $this->_db->setQuery($query);
            $file_allowed = $this->_db->loadResult();
            if ($file_allowed == null)
                $file_allowed = 0; // if there is no entry, it is inherited
        }
        
        if ($this->_selected_category != null) {
            // if i have just check category access
            if ($this->_selected_file == null)
                $file_allowed = 0; // value for inherited from category
            
            $query = 'select `read` from #__docario_category_user_allowed';
            $query = ' where userid='.$this->_user->id;
            $query .= ' and category_id='.$this->_selected_category->id;
            
            $this->_db->setQuery($query);
            $category_user_allowed = $this->_db->loadResult();
            
            if (($category_user_allowed == null) && ($category_user_allowed == 0)) {
                $query = 'select `read` from #__docario_category_group_allowed';
                $query .= 'where category_id='.$this->_selected_category->id;
                $query .= ' and (1=0';
                foreach ($this->_groups as $group) {
                    $query .= ' or group_id='.$group->id;
                }
                
                $this->_db->setQuery($query);
                $reads = $this->_db->loadResultArray();
                foreach ($reads as $read)
                    if ($read) return true;
                return false;
            } else {
                if ($category_user_allowed == 1) return true;
                else return false;
            }
        }
        
        if ($file_allowed != null and $file_allowed == 2) return false;
        else return true;
    }
    
    function canWrite() {
        if ($this->_selected_file != null) {
            $query = 'select `write` from #__docario_file_user_allowed';
            $query .= ' where userid='.$this->_user->id;
            $query .= ' and file_id='.$this->_selected_file->id;
            
            $this->_db->setQuery($query);
            $file_allowed = $this->_db->loadResult();
            if ($file_allowed == null)
                $file_allowed = 0; // if there is no entry, it is inherited
        }
        
        if ($this->_selected_category != null) {
            // if i have just check category access
            if ($this->_selected_file == null)
                $file_allowed = 0; // value for inherited from category
            
            $query = 'select `write` from #__docario_category_user_allowed';
            $query = ' where userid='.$this->_user->id;
            $query .= ' and category_id='.$this->_selected_category->id;
            
            $this->_db->setQuery($query);
            $category_user_allowed = $this->_db->loadResult();
            
            if (($category_user_allowed == null) && ($category_user_allowed == 0)) {
                $query = 'select `write` from #__docario_category_group_allowed';
                $query .= 'where category_id='.$this->_selected_category->id;
                $query .= ' and (1=0';
                foreach ($this->_groups as $group) {
                    $query .= ' or group_id='.$group->id;
                }
                
                $this->_db->setQuery($query);
                $writes = $this->_db->loadResultArray();
                foreach ($writes as $write)
                    if ($write) return true;
                return false;
            } else {
                if ($category_user_allowed == 1) return true;
                else return false;
            }
        }
        
        if ($file_allowed != null and $file_allowed == 2) return false;
        else return true;
    }
}
?>
